package org.zhigong.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.zhigong.dto.SysResult;
import org.zhigong.dto.SysSearch;
import org.zhigong.entity.Yuangong;
import org.zhigong.service.YuangongService;

import java.util.List;

import static org.zhigong.util.CommonUtil.isEmpty;
import static org.zhigong.util.CommonUtil.isNumeric;

@Controller
public class YuangongController {
    @Autowired
    private YuangongService yuangongService;

    // 只做跳转用,初始化使用json获取数据处理.
    @RequestMapping(value = "/yuangongs", method = RequestMethod.GET)
    public String init(){
        return "yuangong";
    }

    // 分页查询获取列表,返回json
    @RequestMapping(
            value = "/yuangongs",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<List<Yuangong>> search(SysSearch sysSearch){
        Yuangong yuangong = new Yuangong();
        String condS = sysSearch.getCondition();
        yuangong.setXm(condS);
        yuangong.getPage().setCurrentPage(sysSearch.getCurrentPage());
        yuangong.getPage().setPageNumber(sysSearch.getPageNumber());
        SysResult<List<Yuangong>> sysResult;
        try{
            List<Yuangong> yuangongList = yuangongService.getYuangongList(yuangong);
            if(yuangongList.size() > 0){
                yuangongList.get(0).setPage(yuangong.getPage());
            }
            sysResult = new SysResult<List<Yuangong>>(true, yuangongList);
        }catch (Exception e){
            sysResult = new SysResult<List<Yuangong>>(false, e.getMessage());
        }
        return sysResult;
    }

    // 新增
    @RequestMapping(
            value = "/yuangongs",
            method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Integer> create(@RequestBody Yuangong yuangong){
        SysResult<Integer> sysResult;
        try{
            Boolean add  = yuangongService.addYuangong(yuangong);
            sysResult = new SysResult<Integer>(true, add ? 1 : 0);
        }catch (Exception e){
            sysResult = new SysResult<Integer>(false, e.getMessage());
        }
        return sysResult;
    }


    // 删除
    @RequestMapping(
            value = "/yuangongs/{ygbh}",
            method = RequestMethod.DELETE,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Integer> delete(@PathVariable("ygbh") Long ygbh){
        SysResult<Integer> sysResult;
        try{
            Boolean del = yuangongService.deleteYuangong(ygbh);
            sysResult = new SysResult<Integer>(true, del ? 1 : 0);
        }catch(Exception e){
            sysResult = new SysResult<Integer>(false, e.getMessage());
        }
        return sysResult;
    }

    // 更新
    @RequestMapping(
            value = "/yuangongs/{ygbh}",
            method = RequestMethod.PUT,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Integer> update(@RequestBody Yuangong yuangong){
        SysResult<Integer> sysResult;
        try{
            Boolean upd = yuangongService.updateYuangong(yuangong);
            sysResult = new SysResult<Integer>(true, upd ? 1 : 0);
        }catch(Exception e){
            sysResult = new SysResult<Integer>(false, e.getMessage());
        }
        return sysResult;
    }

    // 查看
    @RequestMapping(
            value = "/yuangongs/{ygbh}",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Yuangong> view(@PathVariable("ygbh") Long ygbh){
        SysResult<Yuangong> sysResult;
        try{
            Yuangong yuangong = yuangongService.getYuangongByYgbh(ygbh);
            sysResult = new SysResult<Yuangong>(true, yuangong);
        }catch(Exception e){
            sysResult = new SysResult<Yuangong>(false, e.getMessage());
        }
        return sysResult;
    }
}
